﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MQC.board;
using System.Drawing;

namespace MQC
{
    public partial class board2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            List<DataType> list = new List<DataType>();
            using (DbAppDataContext db = new DbAppDataContext())
            {
                var data = from m in db.Record
                           where m.Del == 0 && (m.FaStatues == 0 || m.FaStatues == 1 || ((m.FaStatues == 2 || m.FaStatues == 3) && Convert.ToDouble((dt - (DateTime)m.FaChangeDate).TotalHours) <= 8))
                           select new DataType { Batch = m.Batch, Maktx = m.Maktx, Mold = m.Mold, Status = m.Status, Machine = m.Machine, ChangeDate = (DateTime)m.ChangeDate, FAChangeDate = (DateTime)m.FaChangeDate, FAStatus = (int)m.FaStatues, AllowProduce = (int)m.AllowProduce };
                list = data.ToList();
                bool sound = IsNoSound(list);
                if (sound) { Sound1.Attributes.Add("autoplay", "autoplay");
                Sound1.Attributes.Add("src", "../alertsound.mp3");
                }
                else { Sound1.Attributes.Remove("autoplay");
                Sound1.Attributes.Remove("src");
                }
                Bind(ref list);
                GridView1.DataSource = list;
                GridView1.Columns[3].HeaderStyle.CssClass = "hide";
                GridView1.Columns[3].ItemStyle.CssClass = "hide";
                GridView1.Columns[4].HeaderStyle.CssClass = "hide";
                GridView1.Columns[4].ItemStyle.CssClass = "hide";
                GridView1.Columns[5].HeaderStyle.CssClass = "hide";
                GridView1.Columns[5].ItemStyle.CssClass = "hide";
                GridView1.Columns[6].HeaderStyle.CssClass = "hide";
                GridView1.Columns[6].ItemStyle.CssClass = "hide";
                GridView1.Columns[7].HeaderStyle.CssClass = "hide";
                GridView1.Columns[7].ItemStyle.CssClass = "hide";
                GridView1.Columns[11].HeaderStyle.CssClass = "hide";
                GridView1.Columns[11].ItemStyle.CssClass = "hide";
#if !DEBUG
                GridView1.Columns[8].HeaderStyle.CssClass = "hide";
                GridView1.Columns[8].ItemStyle.CssClass = "hide";
                GridView1.Columns[13].HeaderStyle.CssClass = "hide";
                GridView1.Columns[13].ItemStyle.CssClass = "hide";
#endif
                GridView1.DataBind();
                ColorGridView(ref GridView1);


            }
        }
        public bool IsNoSound(List<DataType> list)
        {
            bool result = false;
            foreach (var n in list)
            {
                if (n.FAStatus == 2 || n.FAStatus == 3)
                {
                    TimeSpan ts = DateTime.Now - n.FAChangeDate;
                    double time = ts.TotalSeconds;
                    if (time <= 30) { result = true; break; }
                }
            }
            return result;
        }
        public void Bind(ref List<DataType> list)
        {
            foreach (var n in list)
            {
                if (n.Status == 0)
                {
                    var x = DateTime.Now - n.ChangeDate;
                    n.Wait = CalTimeToString(x);
                }
                else if (n.Status == 1)
                {
                    var date = GetTime(n.Batch, 1, "MQC");
                    var x = DateTime.Now - date;
                    n.Start = CalTimeToString(x);
                }
                else if (n.Status == 2)
                {
                    var date = GetTime(n.Batch, 2, "MQC");
                    var x = DateTime.Now - date;
                    n.Back = CalTimeToString(x);
                }
                else
                {
                    var date = GetTime(n.Batch, 3, "MQC");
                    var x = DateTime.Now - date;
                    n.Ok = CalTimeToString(x);
                }
                if (n.FAStatus == 0)
                {
                    var x = DateTime.Now - n.FAChangeDate;
                    n.FAWait = CalTimeToString(x);
                }
                else if (n.FAStatus == 1)
                {
                    var date = GetTime(n.Batch, 1, "FA");
                    var x = DateTime.Now - date;
                    n.FAStart = CalTimeToString(x);
                }
                else if (n.FAStatus == 2)
                {
                    var date = GetTime(n.Batch, 2, "FA");
                    var x = DateTime.Now - date;
                    n.FABack = CalTimeToString(x);
                }
                else if (n.FAStatus == 3)
                {
                    var date = GetTime(n.Batch, 3, "FA");
                    var x = DateTime.Now - date;
                    n.FAOk = CalTimeToString(x);
                }
            }
        }
        public string CalTimeToString(TimeSpan ts)
        {
            string result = "";
            double time = Math.Round(ts.TotalHours, 1);
            result = time.ToString();
            return result;
        }
        public bool CalTimeToBool(DateTime dt)
        {
            TimeSpan time;
            double result;
            bool value;
            time = DateTime.Now - dt;
            result = Math.Round(time.TotalMinutes, 1);
            if (result <= 30)
            {
                value = true;
            }
            else
            {
                value = false;
            }
            return value;
        }
        public DateTime GetTime(string batch, int toStatus, string department)
        {
            DateTime result;
            using (DbAppDataContext db = new DbAppDataContext())
            {
                var temp = from m in db.Record_Log
                           where batch == m.Batch && toStatus == m.ToStatus && m.Department == department
                           select m;
                var data = temp.ToList();
                result = (DateTime)data[data.Count - 1].DateTime;
                return result;
            }

        }
        public void IsNoShowColor(ref GridView gridview, int row, int col, string cssName)
        {
            DateTime lastTime1, lastTime2;
            gridview.Rows[row].Cells[col].CssClass = cssName;
            lastTime1 = Convert.ToDateTime(gridview.Rows[row].Cells[8].Text);
            lastTime2 = Convert.ToDateTime(gridview.Rows[row].Cells[13].Text);
            if (col >= 3 && col <= 6 && lastTime1 > lastTime2)
            {
                if (CalTimeToBool(lastTime1))
                {
                    gridview.Rows[row].Cells[0].BackColor = Color.FromArgb(244,187,82);
                }
            }
            else if (col >= 11 && col <= 12 && lastTime1 < lastTime2)
            {
                if (CalTimeToBool(lastTime2))
                {
                    gridview.Rows[row].Cells[0].BackColor = Color.FromArgb(244, 187, 82);
                }
            }
        }
        /// <summary>
        /// 判断gridview单元格显示颜色
        /// </summary>
        /// <param name="gridview"></param>
        public void ColorGridView(ref GridView gridview)
        {
            for (int i = 0; i < gridview.Rows.Count; i++)
            {
                //if (gridview.Rows[i].Cells[3].Text != "&nbsp;")
                //{
                //    IsNoShowColor(ref gridview, i, 3, "hide");
                //}
                //else if (gridview.Rows[i].Cells[4].Text != "&nbsp;")
                //{
                //    IsNoShowColor(ref gridview, i, 4, "hide");
                //}
                //else if (gridview.Rows[i].Cells[5].Text != "&nbsp;")
                //{
                //    IsNoShowColor(ref gridview, i, 5, "hide");
                //}
                //else//6
                //{
                //    IsNoShowColor(ref gridview, i, 6, "hide");
                //}
                //if (gridview.Rows[i].Cells[7].Text == "1")
                //{
                //    IsNoShowColor(ref gridview, i, 7, "hide");
                //}
                if (gridview.Rows[i].Cells[11].Text != "&nbsp;")
                {
                    IsNoShowColor(ref gridview, i, 11, "hide");
                }
                else if (gridview.Rows[i].Cells[12].Text != "&nbsp;")
                {
                    IsNoShowColor(ref gridview, i, 12, "yellow  gird2");
                }
                gridview.Rows[i].Cells[7].Text = "";
            }
        }
    }
}